package cn.itcast.contorller.module;

import cn.itcast.base.BaseContorller;
import cn.itcast.domain.Module;
import cn.itcast.service.module.ModuleService;
import cn.itcast.utils.UtilFuns;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;
import java.util.UUID;

@RequestMapping(value = "/system/module")
@Controller
public class ModuleController extends BaseContorller {
    @Autowired
    private ModuleService moduleService;
    @RequestMapping(value = "/list.do",name ="模块的分页全部查询")
    public String findAllModule(@RequestParam(defaultValue ="1")int pageNum,@RequestParam(defaultValue = "5") int pageSize){
        PageInfo allModule = moduleService.findAllModule(pageNum, pageSize);
        request.setAttribute("pb",allModule);
        return "system/module/module-list";
    }
    @RequestMapping(value = "/toAdd",name = "去新增页面")
    public String toAdd(){
       List<Module> list= moduleService.findAll();
       request.setAttribute("menus",list);
        return "system/module/module-add";
    }
    @RequestMapping(value = "/edit",name = "新增模块/修改模块")
    public String edit(Module module){
        if (UtilFuns.isEmpty(module.getId())){
            module.setId(UUID.randomUUID().toString());
            moduleService.save(module);
        }else {
            moduleService.update(module);
        }
        return "redirect:/system/module/list.do";
    }
    @RequestMapping(value = "/toUpdate",name = "修改的页面的回显")
    public  String toUpdate(String id){
   Module module= moduleService.findById(id);
   request.setAttribute("module",module);
        List<Module> all = moduleService.findAll();
        request.setAttribute("menus",all);
        return "system/module/module-update";
    }
    @RequestMapping(value = "/delete",name = "删除模块权限")
    public String delete(String id){
    moduleService.delete(id);
        return "redirect:/system/module/list.do";
    }





}
